<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="热词" prop="keywords">
        <el-input v-model="dataForm.keywords" placeholder="热词"></el-input>
      </el-form-item>
      <el-form-item label="平台类型">
        <el-select v-model="dataForm.hotSrc" placeholder="平台类型" clearable>
		  <el-option label="全部" value=""></el-option>
		  <el-option label="首页" value="1"></el-option>
		  <el-option label="福利商城" value="2"></el-option>
          <el-option label="联盟店" value="4"></el-option>
          <el-option label="联盟店商品" value="5"></el-option>
          <el-option label="采购" value="6"></el-option>
		</el-select>
	  </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="dataForm.typeId" placeholder="类型" clearable>
		  <el-option label="全部" value=""></el-option>
		  <el-option label="热词" value="1"></el-option>
		  <el-option label="内部广告" value="2"></el-option>
		</el-select>
	  </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('banner:addBanner')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('banner:removeBanner')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="添加时间">
      </el-table-column>
      <el-table-column
        prop="hotWords"
        header-align="center"
        align="center"
        label="热词">
      </el-table-column>
      <el-table-column
        prop="hotSrc"
        header-align="center"
        align="center"
        label="平台类型">
        <template slot-scope="scope">
          <span v-if="scope.row.hotSrc === 1">首页</span>
          <span v-if="scope.row.hotSrc === 2">福利商城</span>
          <span v-if="scope.row.hotSrc === 4">联盟店</span>
          <span v-if="scope.row.hotSrc === 5">联盟店商品</span>
          <span v-if="scope.row.hotSrc === 6">采购</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="hotType"
        header-align="center"
        align="center"
        label="热词类型">
        <template slot-scope="scope">
          <span v-if="scope.row.hotType === 1">文字</span>
          <span v-if="scope.row.hotType === 2">外链</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="typeId"
        header-align="center"
        align="center"
        label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.typeId === 1">热词</span>
          <span v-if="scope.row.typeId === 2">内部广告</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="hotSort"
        header-align="center"
        align="center"
        label="排序(倒序)">
      </el-table-column>
      <el-table-column
        prop="hotClick"
        header-align="center"
        align="center"
        label="点击量">
      </el-table-column>
      <el-table-column
        prop="imgName"
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope="scope">
          <span v-if="scope.row.logoImg !== null && scope.row.logoImg !== ''">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="'http://image.yilianbaihui.cn/consumer/' + scope.row.logoImg" style="max-height: 160px;max-width: 200px">
            <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/' + scope.row.logoImg" style="max-height: 50px;max-width: 80px">
          </el-popover>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="hotSale"
        header-align="center"
        align="center"
        label="显示状态">
        <template slot-scope="scope">
          <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.hotSale"
            :active-value= 1
            :inactive-value= 0
            @change="batch(scope.row.id, scope.row.hotSale, scope.row.typeId, scope.row.hotSrc)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './hotwords-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          hotSrc: '',
          typeId: '',
          keywords: ''
        },
        num: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/search/queryHotWordsList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'hotSrc': this.dataForm.hotSrc,
            'typeId': this.dataForm.typeId,
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
        // 查询各类型上架的数量
        this.$http({
          url: this.$http.adornUrl('/search/queryTypeNum'),
          method: 'post',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.num = data.data
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/search/removeHotWordsById'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batch (id, hotSale, typeId, hotSrc) {
        // if (hotSrc === 1 && typeId === 1 && this.num[0] >= 10) {
        //   this.$message.error('当前首页展示热词最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 2 && typeId === 1 && this.num[1] >= 10) {
        //   this.$message.error('当前福利商城展示热词最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 3 && typeId === 1 && this.num[2] >= 10) {
        //   this.$message.error('当前兑换商城展示热词最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 4 && typeId === 1 && this.num[3] >= 10) {
        //   this.$message.error('当前联盟店展示热词最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 5 && typeId === 1 && this.num[4] >= 10) {
        //   this.$message.error('当前联盟店商品展示热词最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 1 && typeId === 2 && this.num[5] >= 10) {
        //   this.$message.error('当前首页展示内部广告最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 2 && typeId === 2 && this.num[6] >= 10) {
        //   this.$message.error('当前福利商城展示内部广告最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 3 && typeId === 2 && this.num[7] >= 10) {
        //   this.$message.error('当前兑换商城展示内部广告最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 4 && typeId === 2 && this.num[8] >= 10) {
        //   this.$message.error('当前联盟店展示内部广告最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        // if (hotSrc === 5 && typeId === 2 && this.num[9] >= 10) {
        //   this.$message.error('当前联盟店商品展示内部广告最多不得超过十个')
        //   this.getDataList()
        //   return false
        // }
        this.$confirm(`确定对该热词进行显示操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/search/modifyUpperAndLowerRacksById/${id}/${hotSale}`),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
